﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using RiskMan.Config;
using RiskMan.DataClass.Models;
using System.Windows.Forms;
using System.Collections.ObjectModel;
using System.Reflection;
using RiskMan.DataClass.Models.RM;

namespace RiskMan.Views.RMInvestor
{
    /// <summary>
    /// Логика взаимодействия для ViewAddRMInvestorItem.xaml
    /// </summary>
    /// 
    public partial class ViewAddRMInvestorItem : Window
    {
        #region static

        public static DialogResult Show ( ModuleInvestorModel item, bool edit )
        {
            return ( new ViewAddRMInvestorItem( item, edit ) ).Result;
        }

        #endregion

        public DialogResult Result = System.Windows.Forms.DialogResult.Cancel;
        public ModuleInvestorModel ResultItem = new ModuleInvestorModel( );

        private bool _edit = false;
        private ModuleInvestorModel _item;
        private List<Trade> trades = new List<Trade>( );

        public ViewAddRMInvestorItem ( ModuleInvestorModel item, bool edit )
        {
            InitializeComponent( );
            _edit = edit;
            _item = item;

            LoadData( );

            ShowDialog( );
        }

        #region загрузка данных в форму

        public void LoadData ( )
        {
            if ( _edit == false )
            {
                
                textBoxDrowDown.Text = "10000";
                textBoxStartDeposite.Text = "100000";
                var data = MainWindow.StaticRiskMan._dataProvider;

                trades = data.GetTradeList( );

                for ( int j = 0; j < trades.Count; j++ )
                {
                    for ( int i = 0; i < data.RmInvestorItems.Count; i++ )
                    {
                        if ( data.RmInvestorItems [ i ].Account == trades [ j ].Account &&
                            data.RmInvestorItems [ i ].ClientCode == trades [ j ].ClientCode )
                        {
                            trades.RemoveAt( j );
                        }
                    }
                }
                

                comboBoxAccount.ItemsSource = trades.Select( a => a.ClientCode + "_" + a.Account );

                comboBoxTypeCalc.ItemsSource = new TypeCalcClass( );
                comboBoxTypeLimit.ItemsSource = new TypeLimiteClass( );
            }
            else
            {
                TextBoxComisBlock.Text = _item.ComisBlock.ToString( );
                CheckBoxComisBlock.IsChecked = _item.RunComicBlock;
                CheckBox_Consider_VM_ND.IsChecked = _item.ConsiderVMND;

                textBoxDrowDown.Text = _item.Drowdown.ToString( );
                textBoxStartDeposite.Text = _item.Deposite.ToString( );
                var data = MainWindow.StaticRiskMan._dataProvider;

                #region account, clientcode

                trades = data.GetTradeList( );

                for ( int j = 0; j < trades.Count; j++ )
                {
                    for ( int i = 0; i < data.RmInvestorItems.Count; i++ )
                    {
                        if ( data.RmInvestorItems [ i ].Account == trades [ j ].Account &&
                            data.RmInvestorItems [ i ].ClientCode == trades [ j ].ClientCode )
                        {
                            trades.RemoveAt( j );
                            if ( j == 0 )
                                j = 0;
                            else
                                j--;
                        }
                    }
                }
                trades.Add( new Trade( ) { Account = _item.Account, ClientCode = _item.ClientCode } );
                comboBoxAccount.ItemsSource = trades.Select( a => a.ClientCode + "_" + a.Account );

                int index = 0;
                foreach ( var tmp in trades )
                {
                    if ( tmp.ClientCode == _item.ClientCode &&
                         tmp.Account == _item.Account )
                    {
                        comboBoxAccount.SelectedIndex = index;
                    }
                    index++;
                }

                #endregion

                #region typecalc

                comboBoxTypeCalc.ItemsSource = new TypeCalcClass( );
                comboBoxTypeCalc.SelectedIndex = _item.TypeCalc == new TypeCalcClass( ) [ 0 ] ? 0 : 1;

                #endregion

                #region typelimite

                comboBoxTypeLimit.ItemsSource = new TypeLimiteClass( );
                comboBoxTypeLimit.SelectedIndex = _item.TypeLimite == new TypeLimiteClass( ) [ 0 ] ? 0 : 1;

                #endregion

                checkBoxGO.IsChecked = _item.TypeGO == 1 ? false : true;
            }
            

        }

        #endregion

        private void Window_Loaded ( object sender, RoutedEventArgs e )
        {

        }

        #region отмена изменений

        private void buttonCancel_Click ( object sender, RoutedEventArgs e )
        {
            Result = System.Windows.Forms.DialogResult.Cancel;
            Close( );
        }

        #endregion

        #region сохранение изменений

        private void buttonSave_Click ( object sender, RoutedEventArgs e )
        {
            try
            {
                if ( comboBoxAccount.SelectedIndex >= 0 ||
                    comboBoxTypeCalc.SelectedIndex >= 0 ||
                    comboBoxTypeLimit.SelectedIndex >= 0 )
                {
                    ResultItem.Account = trades [ comboBoxAccount.SelectedIndex ].Account;
                    ResultItem.ClientCode = trades [ comboBoxAccount.SelectedIndex ].ClientCode;
                    ResultItem.Check = true;
                    ResultItem.Deposite = Convert.ToDouble( textBoxStartDeposite.Text );
                    ResultItem.Drowdown = Convert.ToDouble( textBoxDrowDown.Text );
                    ResultItem.Status = StatusEnum.WORK;
                    ResultItem.TypeCalc = new TypeCalcClass( ) [ comboBoxTypeCalc.SelectedIndex ];
                    ResultItem.TypeLimite = new TypeLimiteClass( ) [ comboBoxTypeLimit.SelectedIndex ];
                    ResultItem.TypeGO = checkBoxGO.IsChecked == false ? 1 : 2;

                    ResultItem.ConsiderVMND = CheckBox_Consider_VM_ND.IsChecked.Value;
                    ResultItem.RunComicBlock = CheckBoxComisBlock.IsChecked.Value;
                    if (CheckBoxComisBlock.IsChecked == true)
                        ResultItem.ComisBlock = Convert.ToDouble(TextBoxComisBlock.Text);
                    else
                        ResultItem.ComisBlock = 0;
                }
                else
                    throw new Exception( "Проверьте корректность введенных данных" );

                if ( _edit == false )
                    MainWindow.StaticRiskMan._dataProvider.RmInvestorItems.Add( ResultItem );

                Result = System.Windows.Forms.DialogResult.OK;
                Close( );
            }
            catch ( Exception ex )
            {
                cfg.SetLog( ex.Message, MainWindow.StaticRiskMan.ListBox_Logs, GetType(), MethodBase.GetCurrentMethod() );
            }
        }

        #endregion
    }

    internal class TypeCalcClass : ObservableCollection<String>
    {
        public TypeCalcClass ( )
        {
            Add( "Рубли" );
            Add( "Проценты" );
        }
    }

    internal class TypeLimiteClass : ObservableCollection<String>
    {
        public TypeLimiteClass ( )
        {
            Add( "Обычный" );
        }
    }
}
